﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ProjectWebNhac.BUS;
using System.Data;
using ProjectWebNhac.DTO;

namespace ProjectWebNhac.GUI.User
{
    public partial class ChitietVideo : System.Web.UI.Page
    {
        int MaVideo = 0;
        VideoBus videoBus = new VideoBus();
        BinhLuanVideoBus blVideoBus = new BinhLuanVideoBus();
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request["MaVideo"]!=null)
            {
                MaVideo = Convert.ToInt16(Request["MaVideo"]);
            }
            else
            {
                Response.Redirect("TrangChu.aspx");
            }
            if (!Page.IsPostBack)
            {
                LoadChiTietVideo();
                LoadInfo();
                LoadBinhLuan();
                txtComment.Text = "";
            }
        }

        private void LoadChiTietVideo()
        {
            reDetailVideo.DataSource = videoBus.LayChiTietVideo(MaVideo);
            reDetailVideo.DataBind();

            reVideoCungCaSi.DataSource = videoBus.LayVideoCungCaSiTheoVideo(MaVideo);
            reVideoCungCaSi.DataBind();

            reThongTinCaSi.DataSource = videoBus.LayChiTietCaSiTheoVideo(MaVideo);
            reThongTinCaSi.DataBind();
        }
        private void LoadInfo()
        {
            DataTable dt = videoBus.LayChiTietVideo(MaVideo);
            lbTenVideo.Text = dt.Rows[0]["TenVideo"].ToString();
            DateTime date = DateTime.Parse(dt.Rows[0]["NgayThem"].ToString());
            string demo= date.ToString("dd/MM/yyyy");
            demo = demo.Replace(':', '-');
            lbNgayDang.Text = demo;
            hpCaSi.Text = dt.Rows[0]["TenCaSi"].ToString();
            hpCaSi.NavigateUrl = "ChiTietCaSi.aspx?MaCaSi=" + dt.Rows[0]["MaCaSi"].ToString();
            hpUploader.Text = dt.Rows[0]["TaiKhoan"].ToString();
            hpUploader.NavigateUrl = "#";
            lbLuotNghe.Text = dt.Rows[0]["LuotXem"].ToString();
        }
        private void LoadBinhLuan()
        {
            reCommentList.DataSource = blVideoBus.
                LayBinhLuanTheoMaVideo(MaVideo);
            reCommentList.DataBind();
        }

        protected void btnBinhLuan_Click(object sender, EventArgs e)
        {
            NguoiDungDTO objNguoidung = (NguoiDungDTO)Session["ThanhVien"];
            BinhLuanVideoDTO objBinhLuan = new BinhLuanVideoDTO();
            objBinhLuan.TaiKhoan = objNguoidung.TaiKhoan;
            objBinhLuan.MaVideo = MaVideo;
            objBinhLuan.LoiBinh = txtComment.Text;
            if (blVideoBus.ThemBinhLuanVideo(objBinhLuan))
            {
                LoadBinhLuan();
            }
        }
    }
}